﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Candy;
//调试用
using System.Windows.Forms;
namespace JYoperation.Class
{
    public class JYkdef
    {
        private List<string> strTalk = new List<string>();
        private List<kdefdata> ListKdef = new List<kdefdata>();
        private JYpath tmpJYpath;
        private int PtrIndex;
        private class kdefdata
        {
            public List<short> Data;
            public kdefdata()
            {
                Data = new List<short>();
            }
        }
        public JYkdef(JYpath jypath)
        {
            tmpJYpath = jypath;
            ReadTalk();
            ReadKdef();
        }
        private void ReadTalk()
        {
            List<int> talkidx = new List<int>();
            //read idx
            using (FileStream fs = new FileStream(tmpJYpath.FileTalkIdx, FileMode.Open))
            {
                using (BinaryReader br = new BinaryReader(fs))
                {
                    talkidx.Add(0);
                    for (int i = 0; i < fs.Length / 4; i++)
                    {
                        talkidx.Add(br.ReadInt32());
                    }
                }
            }
            //read grp
            using (FileStream fs = new FileStream(tmpJYpath.FileTalkgrp, FileMode.Open))
            {
                using (BinaryReader br = new BinaryReader(fs))
                {
                    for (int i = 1; i < talkidx.Count; i++)
                    {
                        List<byte> tmpByte = new List<byte>();
                        for (int j = 0; j < talkidx[i] - talkidx[i - 1]; j++)
                        {
                            tmpByte.Add((byte)(br.ReadByte()^0xff));
                        }
                        string tmpstring = sfeadd.Str.Big5ToUnicode(tmpByte.ToArray());
                        
                        strTalk.Add(sfeadd.Str.TraditionToSimple(tmpstring));
                    }
                }
            }
        }
        private void ReadKdef()
        {
            List<int> kdefidx = new List<int>();
            //read idx
            using (FileStream fs = new FileStream(tmpJYpath.FileKdefIdx, FileMode.Open))
            {
                using (BinaryReader br = new BinaryReader(fs))
                {
                    kdefidx.Add(0);
                    for (int i = 0; i < fs.Length / 4; i++)
                    {
                        kdefidx.Add(br.ReadInt32());
                    }
                }
            }
            //read grp
            using (FileStream fs = new FileStream(tmpJYpath.FileKdefgrp, FileMode.Open))
            {
                using (BinaryReader br = new BinaryReader(fs))
                {
                    for (int i = 1; i < kdefidx.Count; i++)
                    {
                        //List<byte> tmpByte = new List<byte>();
                        kdefdata tmpkdef = new kdefdata();
                        for (int j = 0; j < kdefidx[i] - kdefidx[i - 1]; j+=2)
                        {
                            tmpkdef.Data.Add(br.ReadInt16());
                        }
                        ListKdef.Add(tmpkdef);
                    }
                }
            }            
        }
        public bool Run(int KdefIndex)
        {
            kdefdata tmpkdef = ListKdef[KdefIndex];
                
            switch (tmpkdef.Data[PtrIndex++])
            {

                    case 0:
                        break;
                    case 1:
                        //talk
                        //MessageBox.Show(strTalk[tmpkdef.Data[i++]]);
                        Global.uiSurface.RemoveByTag("talk");
                        Global.uiSurface.addRect("talk", null, new Microsoft.Xna.Framework.Rectangle(150, 490, 500, 100));
                        Global.uiSurface.addString("talk", strTalk[tmpkdef.Data[PtrIndex]].Replace('*', '\n'), 155, 495);
                        PtrIndex += 3;
                        //return;
                        break;
                    case 2:
                        break;
             }
            if (PtrIndex >= tmpkdef.Data.Count) return true;
            else return false;
        }
    }
}
